package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppMyProjectClickdetail;
import org.jeecg.modules.demo.hd.entity.AppUserGoldMain;
import org.jeecg.modules.demo.hd.mapper.AppUserGoldMainMapper;
import org.jeecg.modules.demo.hd.service.IAppUserGoldMainService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 用户金币管理
 * @Author: jeecg-boot
 * @Date: 2025-02-04
 * @Version: V1.0
 */
@Service
public class AppUserGoldMainServiceImpl extends ServiceImpl<AppUserGoldMainMapper, AppUserGoldMain> implements IAppUserGoldMainService {




    @Override
    public double queryCurrentGold() {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = currentUser.getId();
        return  queryUserGold(userId);
    }
    @Override
    public double queryUserGold(String userId)
    {
        LambdaQueryWrapper<AppUserGoldMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserGoldMain::getUserId, userId);
        AppUserGoldMain appUserCashMain = this.baseMapper.selectOne(queryWrapper);
        if (appUserCashMain != null) {
            return appUserCashMain.getGoldNum();
        }
        return 0d;
    }

    @Override
    public boolean updateCurrentGold(double goldNum) {
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = currentUser.getId();
        QueryWrapper<AppUserGoldMain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        AppUserGoldMain appUserCashMain = this.baseMapper.selectOne(queryWrapper);
        if (appUserCashMain != null) {
            appUserCashMain.setGoldNum(goldNum);
            this.baseMapper.updateById(appUserCashMain);
        } else {
            AppUserGoldMain main = new AppUserGoldMain();
            main.setUserId(userId);
            main.setGoldNum(goldNum);
            main.setGoldStatus("1");
            this.baseMapper.insert(main);
        }
        return true;
    }



    @Override
    public boolean updateGoldMain(AppMyProjectClickdetail model, Integer goldNum) {
        // 获取用户金币主表实体
        return updateGoldMain(model.getUserId(), goldNum);
//        LambdaQueryWrapper<AppUserGoldMain> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(AppUserGoldMain::getUserId, model.getUserId());
//        AppUserGoldMain appUserGoldMain = this.baseMapper.selectOne(queryWrapper);
//        if (appUserGoldMain == null) {
//            appUserGoldMain = new AppUserGoldMain();
//            appUserGoldMain.setUserId(model.getUserId());
//            appUserGoldMain.setGoldNum((float) goldNum);
//            appUserGoldMain.setGoldStatus("1");
//            this.baseMapper.insert(appUserGoldMain);
//        } else {
//            Float goldNum1 = appUserGoldMain.getGoldNum();
//            appUserGoldMain.setGoldNum(goldNum1 + goldNum);
//            this.baseMapper.updateById(appUserGoldMain);
//        }
//        return true;
    }

    @Override
    @Transactional
    public boolean updateGoldMain(String userId, Integer goldNum) {
        // 获取用户金币主表实体
        LambdaQueryWrapper<AppUserGoldMain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserGoldMain::getUserId, userId);
        AppUserGoldMain appUserGoldMain = this.baseMapper.selectOne(queryWrapper);
        if (appUserGoldMain == null) {
            appUserGoldMain = new AppUserGoldMain();
            appUserGoldMain.setUserId(userId);
            appUserGoldMain.setGoldNum((double) goldNum);
            appUserGoldMain.setGoldStatus("1");
            this.baseMapper.insert(appUserGoldMain);
        } else {
            Double goldNum1 = appUserGoldMain.getGoldNum();
            appUserGoldMain.setGoldNum(goldNum1 + goldNum);
            this.baseMapper.updateById(appUserGoldMain);
        }
        return true;
    }

    @Override
    public void clean() {
        List<AppUserGoldMain> appUserGoldMains = this.baseMapper.selectList(null);
        for (AppUserGoldMain appUserGoldMain : appUserGoldMains) {
            appUserGoldMain.setGoldNum(0.0);
            this.baseMapper.updateById(appUserGoldMain);
        }
    }
}
